<?php

include('lib/src/config.php');

require_once "lib/src/apiClient.php";
require_once "lib/src/contrib/apiCalendarService.php";

$calID = 'stanley5.com_p13b36incoflgq8076i6lvqk0k@group.calendar.google.com';  //Family Tree

$name = "Old Person";
$mob = 05;
$dob = 31;
$yob = 1965;
$currY = date('Y'); //start in the current year

$stDate = mktime(0,0,0,$mob,$dob,$currY);
$endDate = mktime(0,0,0,$mob,date("j", $stDate)+1,$currY); //this will handle probs like 12/31 + 1

$startDate = date("Y-m-d", $stDate);
$endDate = date("Y-m-d", $endDate);

print("$startDate   $endDate\n");

session_start();

//  AUTHENITCATE

$apiClient = new apiClient();
$apiClient->setUseObjects(true);
$service = new apiCalendarService($apiClient);

if (isset($_SESSION['oauth_access_token'])) {
  $apiClient->setAccessToken($_SESSION['oauth_access_token']);
} else {
  $token = $apiClient->authenticate();
  $_SESSION['oauth_access_token'] = $token;
}

?>

<html>

<body>
<p>
Adding event:
</p>

<pre>
<?php

//  CREATE EVENT

$event = new Event();

$event->setSummary("$name ($yob)");

$stDT = new EventDateTime();
$stDT->setDate("$startDate");  //use setDate for date property, setDateTime for dateTime property
$stDT->setTimeZone('America/New_York'); //must set a timezone for a recurring event (?)
$event->setStart($stDT);
$endDT = new EventDateTime();
$endDT->setDate("$endDate");
$endDT->setTimeZone('America/New_York');
$event->setEnd($endDT);

$event->setRecurrence(array('RRULE:FREQ=YEARLY'));

print_r($event);

$newEvent = $service->events->insert($calID, $event);

//  OUTPUT

$evtID = $newEvent->getID();


?>
</pre>

<p>Details for new event <?php print($evtID); ?>:</p>
<pre>

<?php
print_r($newEvent);
?>

</pre>

</body>
</html>
